package cn.jia.spring7.web.servlet;

import cn.jia.spring6.config.RootConfig;
import cn.jia.spring6.config.WebConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;

/**
 * 自定义 DispatcherServlet
 * Created by jia on 2018/4/8.
 */
public class MyDispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer{
    @Override
    protected Class<?>[] getRootConfigClasses() {
        //制定spring的配置类

        return new Class<?>[]{
                RootConfig.class
        };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {

        return new Class<?>[]{
                //制定配置类
                WebConfig.class
        };
    }

    @Override
    protected String[] getServletMappings() {
        //设置该servlet处理的映射路径
        return new String[]{"/"};
    }

    /**
     * 这个方法可以设置好多配置
     * 重载这个方法，重新设置multipart配置
     * @param registration
     */
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        //设置临时存储位置
        registration.setMultipartConfig(new MultipartConfigElement("/tmp/spittr/uploads",1024,2048,0));
        //这是load-on-startup优先级
        registration.setLoadOnStartup(1);
    }

    /**
     * 创建自定义的filter，映射到DispatcherServlet,映射路径就是DispatcherServlet的映射路径
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        return new Filter[]{new MyFilter()};
    }
}
